const {bfmetaSDK} = require("../util/util")
const userModel = require("../model/userModel")
const userEventModel = require("../model/userEventModel")
const boardModel = require("../model/boardModel")


//根据助记词登陆
const login = async (req,res,next) => {
    try {
      const user = await userModel.login(req.body.mnemonic);
      if(user){
        //更新用户的last_login_time
        userModel.updateLastLoginTime(user.id,new Date());
        res.send({
          code: "200",
          msg: "用户登陆成功",
          data: user
        })
      }else{
        res.send({
          code: "403",
          msg: "用户登陆失败",
        })
      }
    } catch (error) {
      next(error)
    }
  }

  //用户注册
  const register = async (req,res,next) => {//前端传name 和 mnemonic
    try {
      const user = await userModel.getUserByName(req.body.name);
      if(user){
        res.send({
          code: "403",
          msg: "用户名重复!!!",
        })
      }else{
        const keypair = await bfmetaSDK.bfchainSignUtil.createKeypair(req.body.mnemonic)
        const address = await bfmetaSDK.bfchainSignUtil.getAddressFromPublicKey(keypair.publicKey)
        const publicKey = keypair.publicKey.toString("hex");
        const secretKey = keypair.secretKey.toString("hex");
        const user_id =await userModel.register(req.body.name,address,req.body.mnemonic,publicKey,secretKey,new Date(),new Date());
        //name,address,mnemonic,public_key,private_key,create_time,last_login_time
        const user = await userModel.getUserById(user_id);
        // console.log(user)
        if(user){
          res.send({
            code: "200",
            msg: "注册成功!",
            data:user
          })
        }else{
          res.send({
            code: "403",
            msg: "注册失败!",
          })
        }
      }
    } catch (error) {
      next(error)
    }
  }

  //用户信息更新
  const updateUser = async (req,res,next) => {
    try{
      const affectedRows = await userModel.updateUser(req.body.id,req.body.name);
      if(affectedRows==0){
        res.send({
          code: "403",
          msg: "更新用户信息失败!",
        })
      }else{
        res.send({
          code: "200",
          msg: "更新成功!",
        })
      }
    } catch (error){
      next(error)
    }
  }

  //用户删除
  const deleteUser = async (req,res,next) => {//外键坑 删除顺序！！
    try{
      const affectedRows1 = await userEventModel.deleteByUserId(req.body.id);//删除用户绑定的投票事件记录
      const affectedRows2 = await userModel.deleteUser(req.body.id);//删除用户
      if(affectedRows2==0){
        res.send({
          code: "403",
          msg: "删除失败，用户不存在!!",
        })
      }else{
        res.send({
          code: "200",
          msg: "删除成功",
        })
      }
    } catch(error) {
      next(error)
    }
  }

  //用户投票
  const vote = async (req,res,next) => {
    try{
      const {user_id,event_id,board_id} = req.body;

      //1.更新is_voted字段为1 以及board_id
      await userEventModel.updateIsVotedAndBoardId(user_id,event_id,board_id);
      //2.候选人票数+1
      await boardModel.updateTotalAmount(board_id);
      //3.获取投票人 可返回给前端
      
      res.send({//返回用户信息给前端
        code: "200",
        msg: "投票成功!",
      })
    } catch (error){
      next(error)
    }
  }

module.exports = {login,register, updateUser, deleteUser, vote}
